let { User } = require('../models');
const auth = require('../untils/auth');

let index_fn = async (ctx, next) => {
    let username = ctx.session.userName;
    let obj = {
        username
    }
    ctx.render('index.html', obj)
    console.log(username);
};
let login_fn = async (ctx, next) => {
    ctx.render('login.html');
};
let register_fn = async (ctx, next) => {
    ctx.render('register.html', { msg: '' });
};
let loginDone_fn = async (ctx, next) => {
    // 获取从前端传来的数据（用户信息）
    let uid = ctx.request.body.username || '';
    let pwd = ctx.request.body.password || '';
    let res = '';

    if (uid.length > 0 && pwd.length > 0) {
        let row = await User.findOne({
            where: {
                username: uid,
                password: pwd
            }
        });
        let u2Json = JSON.stringify(row);
        if (u2Json !== 'null') {

            if (row.state === 2) {
                //管理员
                ctx.session.userId = row.id;
                ctx.session.userName = row.username;
                ctx.render('adminHome.html')
            } else {
                //普通用户
                ctx.session.userId = row.id;
                ctx.session.userName = row.username;
                res = { code: 200, msg: '登录成功' };
            }
        } else {
            res = { code: 1000, msg: '用户名或密码错误，请确认' };
        }

    } else {
        res = { code: 1000, msg: '用户名和密码不能为空' };
    }
    ctx.body = JSON.stringify(res);
};
let registerDone_fn = async (ctx, next) => {
    // 获取从前端传来的数据（用户信息）
    let uid = ctx.request.body.username || '';
    let pwd = ctx.request.body.password || '';
    let confirmpwd = ctx.request.body.repassword || '';

    if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
        let row = await User.findOne({
            where: {
                username: uid
            }
        });
        let u2Json = JSON.stringify(row);
        if (u2Json === 'null') {
            let u1 = await User.create({
                username: uid,
                password: pwd
            });
            let u1Json = JSON.stringify(u1);
            if (u1Json !== 'null') {
                res = { code: 200, msg: '用户注册成功' };
            } else {
                res = { code: 1000, msg: '因为服务器或者网络原因，用户未注册成功' };
            }
        } else {
            res = { code: 1000, msg: '当前用户名已经注册，请确认后重试' };
        }
    } else {
        res = { code: 1000, msg: '用户名和密码不能为空，且两密码应该一致' };
    }
    // 返回消息
    ctx.body = JSON.stringify(res);
};

var fn_admin = async (ctx, next) => {
    ctx.render('adminHome.html');
}


let logout_fn = async (ctx, next) => {
    ctx.session = null;
    ctx.body = { code: 200, msg: '注销成功' };
}
let myInfo_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了我的资料' };
};
let myIssus_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了我的问卷' };
};
let newCreate_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了创建页面' };
};
let questionnaire_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了调查问卷' };
};
let goIndex_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了调查问卷' };
};
let score_fn = async (ctx, next) => {
    ctx.body = { code: 200, msg: '去往了评分页面' };
};

let goMyInfo_fn = async (ctx, next) => {
    ctx.render('myInfo.html')
};
let goMyIssus_fn = async (ctx, next) => {
    ctx.render('myIssus.html')
};
let goNewCreate_fn = async (ctx, next) => {
    ctx.render('newCreate.html')
};
let goScoreSuccess_fn = async (ctx, next) => {
    ctx.render('scoreSuccess.html')
};
module.exports = {
    '/': ['get', auth, index_fn],
    '/goIndex': ['post', auth, goIndex_fn],

    '/login': ['get', auth, login_fn],

    '/logout': ['post', auth, logout_fn],

    '/myInfo': ['post', auth, myInfo_fn],
    '/index/goMyInfo': ['get', goMyInfo_fn],

    '/myIssus': ['post', auth, myIssus_fn],
    '/goMyIssus': ['get', auth, goMyIssus_fn],

    '/questionnaire': ['post', auth, questionnaire_fn],

    '/newCreate': ['post', auth, newCreate_fn],
    '/goNewCreate': ['get', auth, goNewCreate_fn],


    '/goScoreSuccess': ['get', auth, goScoreSuccess_fn],

    '/score': ['post', auth, score_fn],

    '/register': ['get', register_fn],
    '/loginDone': ['post', loginDone_fn],
    '/registerDone': ['post', registerDone_fn],

    '/admin': ['get', fn_admin]
}